import logging

import allure
import jsonpath

from untils.send_request import send_jdbc_request


def json_extractor(case,response,alldata):
    if case["jsonExData"]:
        with allure.step("核心4:json提取"):
            for key, value in eval(case["jsonExData"]).items():
                value = jsonpath.jsonpath(response.json(), value)[0]
                alldata[key] = value
                logging.info("4.json提取，根据{}提取数据，此时的全局变量是：{}".format(case["jsonExData"], alldata))


def jdbc_extractor(case,alldata):
    # 核心步骤四：sql提取
    if case["sqlExData"]:
        with allure.step("核心4:jdbc提取"):
            for key, value in eval(case["sqlExData"]).items():
                value = send_jdbc_request(value)[0]
                alldata[key] = value
                logging.info("4.jdbc提取，根据{}提取数据，此时的全局变量是：{}".format(case["sqlExData"], alldata))